package org.ilovecode.mvc
{
	import com.asual.SWFAddress;
	import com.asual.SWFAddressEvent;
	
	import display.screens.AboutScreen;
	import display.screens.ApartmentsScreen;
	import display.screens.BusinessScreen;
	import display.screens.ContactScreen;
	import display.screens.FaqScreen;
	import display.screens.GarageScreen;
	import display.screens.LayoutScreen;
	import display.screens.SearchScreen;
	import display.screens.StartScreen;
	
	import events.LoadingEvent;
	
	import flash.errors.IllegalOperationError;
	
	import org.ilovecode.events.FaqEvent;
	import org.ilovecode.screen.FloorScreen;
	import org.ilovecode.screen.ScreenName;
	
	import utils.method.capitalizeFirstLetter;

	public class Controller implements IController
	{
		private static var _instance:IController;
		private var model:IModel;
		private var _firstRun:Boolean = true;
		
		public function Controller(lock:Singleton)
		{
			if (!lock) throw new IllegalOperationError("Can't instantiate Singleton class, call getInstance method");
			
			model = Model.getInstance();
		}
		
		public static function getInstance():IController
		{
			if (!_instance) _instance = new Controller(new Singleton());
			
			return _instance;
		}
		
		private function handle_SWFAddressExternalChange(event:SWFAddressEvent):void
		{
			var args:Array = SWFAddress.getValue().split("/");
			//Remove first arg which is empty string
			if (args[0] == "")
				args.splice(0, 1);

			try
			{
				var screenName:String = "screen name " + String(args[0]).toUpperCase();
				
				if (args.length >= 3 && screenName == ScreenName.LAYOUT)
				{
					if (args[1] == "floor")
					{
						showFloor(int(args[2]));
						return;
					}
					else if (args[1] == "apartment")
					{
						showApartment(int(args[2]));
						return;
					}
				}
				
				if (args.length >= 1)
					showScreen(screenName);
				else
					showScreen(ScreenName.START);
			}
			catch (e:Error)
			{
				showScreen(ScreenName.START);
			}
		}
		
		private function handle_SWFAddressChange(event:SWFAddressEvent):void
		{
			if (!model.currentScreen)
				return;
			
			var pageName:String = ScreenName.getLabel(model.currentScreen.type);
			var outputPageName:String = (pageName == "") ? "Početna" : pageName;
			SWFAddress.setTitle(outputPageName + " - Ban Centar");
		}
		
		public function getScreenXML(screenName:String):XML
		{
			var xmls:Array = model.screenXMLs;
			for (var i:int = 0; i < xmls.length; i++) 
			{
				if (XML(xmls[i]).@name == screenName)
					return XML(xmls[i]);
			}
			
			return null;
		}
		
		public function switchFaqItem(id:int, offset:Number):void
		{
			var e:FaqEvent = new FaqEvent(FaqEvent.SWITCH_ITEM);
			e.id = id;
			e.offset = offset;
			model.dispatchEvent(e);
		}
		
		public function showProgressIcon(value:Boolean):void
		{
			if (value)
				model.dispatchEvent(new LoadingEvent(LoadingEvent.SHOW_PROGRESS_ICON));
			else
				model.dispatchEvent(new LoadingEvent(LoadingEvent.HIDE_PROGRESS_ICON));
				
		}
		
		public function showScreen(screenName:String):void
		{
			if (_firstRun) 
			{
				_firstRun = false;
				SWFAddress.addEventListener(SWFAddressEvent.EXTERNAL_CHANGE, handle_SWFAddressExternalChange);
				SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handle_SWFAddressChange);
				return;
			}
			
			var page:String = screenName.replace("screen name ", "").toLowerCase();
			switch(screenName)
			{
				case "":
					//Default page
				case ScreenName.START:
					page = "";
					model.currentScreen = new StartScreen();
					break;
				case ScreenName.ABOUT:
					model.currentScreen = new AboutScreen();
					break;
				case ScreenName.APARTMENTS:
					model.currentScreen = new ApartmentsScreen();
					break;
				case ScreenName.BUSINESS:
					model.currentScreen = new BusinessScreen();
					break;
				case ScreenName.CONTACT:
					model.currentScreen = new ContactScreen();
					break;
				case ScreenName.FAQ:
					model.currentScreen = new FaqScreen();
					break;
				case ScreenName.GARAGE:
					model.currentScreen = new GarageScreen();
					break;
				case ScreenName.LAYOUT:
					model.currentScreen = new LayoutScreen();
					break;
				case ScreenName.SEARCH:
					model.currentScreen = new SearchScreen();
					break;
				default:
					throw new Error("Illegal screen");
					break;
			}
			
			SWFAddress.setValue("/" + page);
		}
		
		public function showFloor(floorNumber:int):void
		{
			//Determine screen to be displayed
			var screen:String;
			if (floorNumber < -1)
			{
				screen = ScreenName.GARAGE;
			}
			else if (floorNumber >= -1 && floorNumber <= 1)
			{
				screen = ScreenName.BUSINESS;
			}
			else if (floorNumber >= 2 && floorNumber <= 7)
			{
				screen = ScreenName.LAYOUT;
			}
			else
			{
				throw new Error("Illegal floor");
			}
			
			showScreen(screen);
			
			FloorScreen(model.currentScreen).showFloor(floorNumber);
			
			SWFAddress.setValue(SWFAddress.getValue() + "/floor/" + floorNumber);
		}
		
		public function showApartment(id:int):void
		{
			showScreen(ScreenName.LAYOUT);
			LayoutScreen(model.currentScreen).showApartment(id);
			
			SWFAddress.setValue(SWFAddress.getValue() + "/apartment/" + id);
		}
	}
}

//Private class for Singleon pattern
class Singleton { public function Singleton() {} }